package cn.ike.web.controller.cargo;

import cn.ike.domain.vo.ContractProductVo;
import cn.ike.service.cargo.ContractProductService;
import cn.ike.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.log4j.Log4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
@Log4j
@RequestMapping("/cargo/contract")
public class OutProductController extends BaseController {

    @Reference
    private ContractProductService contractProductService;

    /**
     * 出货表导出，进入导出页面（可以选择船期时间）
     */
    @RequestMapping("/print")
    public String print() {
        return "cargo/print/contract-print";
    }

    /**
     * 出货表导出 A  普通导出 XSSF
     */
    @RequestMapping("/printExcel")
    @ResponseBody
    public void printExcel(String inputDate) throws IOException {
        //1. 导出第一行
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        // 合并单元格：开始行、结束行、开始列、结束列（0、0、1、8）
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 8));
        // 设置行宽度
        int[] width = {5, 21, 9, 11, 15, 26, 15, 15, 15};
        for (int i = 0; i < width.length; i++) {
            sheet.setColumnWidth(i, 256 * width[i]);
        }
        // 创建第一行
        Row row = sheet.createRow(0);
        // 设置行高
        row.setHeightInPoints(36);
        // 创建第一行第二列
        Cell cell = row.createCell(1);
        // inputDate = 2020-09
        String bigTitle = inputDate.replaceAll("-0", "-").replace("-", "年") + "月份出货表";
        // 设置内容
        cell.setCellValue(bigTitle);
        // 设置单元格样式
        cell.setCellStyle(this.bigTitle(workbook));

        //2. 导出第二行
        String[] titles = {"客户", "订单号", "货号", "数量", "工厂", "工厂交期", "船期", "贸易条款"};
        row = sheet.createRow(1);
        for (int i = 0; i < titles.length; i++) {
            cell = row.createCell(i + 1);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(this.title(workbook));
        }

        //3. 导出数据行
        List<ContractProductVo> list = contractProductService.findByShipTime(inputDate + "%");

        if (list != null && list.size() > 0) {
            int index = 2;
            for (ContractProductVo cp : list) {
                // 创建行，从第3行开始
                row = sheet.createRow(index++);

                cell = row.createCell(1);
                cell.setCellValue(cp.getCustomName());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(2);
                cell.setCellValue(cp.getContractNo());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(3);
                cell.setCellValue(cp.getProductNo());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(4);
                cell.setCellValue(cp.getCnumber());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(5);
                cell.setCellValue(cp.getFactoryName());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(6);
                cell.setCellValue(cp.getDeliveryPeriod());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(7);
                cell.setCellValue(cp.getShipTime());
                cell.setCellStyle(this.text(workbook));

                cell = row.createCell(8);
                cell.setCellValue(cp.getTradeTerms());
                cell.setCellStyle(this.text(workbook));
            }

            //4. 导出下载
            // 设置编码
            response.setCharacterEncoding("UTF-8");
            // 设置下载响应头
            response.setHeader("content-disposition", "attachment;fileName=export.xlsx");
            // 把excel文件流，写入到哪个输出流？ response输出流
            workbook.write(response.getOutputStream());
            workbook.close();
        }
    }

    //大标题的样式
    public CellStyle bigTitle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);                //横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线

        return style;
    }
}
